﻿#region Copyright

// All Material Copyright © 2010 Dynamic Concepts Development Corp., All Rights Reserved.
//      Portions Copyright © 1984-2009 Dynamic Concepts Development Corp., All Rights Reserved.
// For further information please contact: SoftwareSupport@dynconcepts.com
// Dynamic Concepts Development Corp.  311 W 57th St. Suite #422, New York, NY 10019

#endregion

#region Using References

using System;
using System.Collections.Generic;
using System.Windows.Input;

#endregion

namespace DynConDevCorp.OpenArch.DynAppFramework.Tiers.UserInterface.WPF.Engine
{
    public class UICommandManager
    {
        #region Public Properies and Indexers

        public static UICommandManager GlobalInstance
        {
            get { return sr_GlobalInstance; }
        }

        #endregion

        #region Public Methods

        public void CommandExecuted(object sender, RoutedUICommand command)
        {
            Type associatedControlType;
            if (r_CommandWindowMap.TryGetValue(command.Name, out associatedControlType))
            {
                Fire_UIElementRequested(associatedControlType);
            }
        }

        public void Register(ICommandHandler commandHandler, Type associatedControlType)
        {
            Type registeredControlType;
            if (r_CommandWindowMap.TryGetValue(commandHandler.Command.Name, out registeredControlType))
            {
                if (registeredControlType != associatedControlType)
                    throw new Exception("Attempt to Register Conflicting UI Elements for Command");
            }
            else
            {
                r_CommandWindowMap.Add(commandHandler.Command.Name, associatedControlType);
            }
        }

        #endregion

        #region Public Events

        public event EventHandler<ShowUIElementEventArgs> UIElementRequested;

        #endregion

        #region Private Methods

        private void Fire_UIElementRequested(Type uiElementType)
        {
            EventHandler<ShowUIElementEventArgs> shadow = UIElementRequested;
            if (shadow != null)
                shadow(this, new ShowUIElementEventArgs(uiElementType));
        }

        #endregion

        #region Static Fields and Constants

        private static readonly UICommandManager sr_GlobalInstance = new UICommandManager();

        #endregion

        #region Instance Fields (ReadOnly)

        private readonly Dictionary<String, Type> r_CommandWindowMap = new Dictionary<String, Type>();

        #endregion

        #region Nested type: ShowUIElementEventArgs

        public class ShowUIElementEventArgs : EventArgs
        {
            #region Constructors

            public ShowUIElementEventArgs(Type uiElementType)
            {
                r_UIElementType = uiElementType;
            }

            #endregion

            #region Public Properies and Indexers

            public Type UIElementType
            {
                get { return r_UIElementType; }
            }

            #endregion

            #region Instance Fields (ReadOnly)

            private readonly Type r_UIElementType;

            #endregion
        }

        #endregion
    }
}